/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.service.*;
import br.com.foxline.clinicplus.util.Caracter;
import br.com.foxline.clinicplus.util.Msg;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.component.tabview.TabView;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerSearchRecords implements Serializable{
    
    
    @EJB
    private CustomerService customerService;
    @EJB
    private RecordsService recordsService;
    @EJB
    private AnnexService annexService;
    @EJB
    private ProcedureService procedureService;
    @EJB
    private DoctorService doctorService;
    @EJB
    private DiaryService diaryService;
    private Doctor doctor;
    private Procedure procedure;
    private Procedure procedureNewRecords;
    private Diagnostic diagnostic;
    private Customer customer;
    private Evolution evolution;
    private List<Evolution> evolutions;
    private Records records;
    private Anamnese anamnese;
    private PhysicalExamination physicalExamination;
    private List<Annex> annexs;
    private List<Anamnese> anamneses;
    private List<Records> recordses;
    private List<Customer> customers;
    private TabView tabView;
    private Diary lastDiary;
    private Records lastRecords;
    
    @PostConstruct
    public void init() {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String customerid = params.get("c");
        String procedureid = params.get("p");
        String doctorid = params.get("d");
        this.procedureNewRecords = new  Procedure();
        this.customers = new ArrayList<Customer>();
        
        customer = new Customer();
        tabView = new TabView();

        if (doctorid != null) {
            try {
                doctor = doctorService.find(Long.parseLong(doctorid));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (customerid != null) {
             
            try {
                customer = customerService.find(Long.parseLong(customerid));
                getHistoryRecords();
                lastDiary = diaryService.getLastDiary(customer);
            } catch (Exception e) {
                Msg.messagemError("Paciente não encontrado !");
                e.printStackTrace();
            }

            if (procedureid != null) {
                procedure = procedureService.find(Long.parseLong(procedureid));
            } else {
                procedure = new Procedure();
            }

            records = new Records();
            anamnese = new Anamnese();
            physicalExamination = new PhysicalExamination();
            diagnostic = new Diagnostic();
            annexs = new ArrayList<Annex>();
            evolution = new Evolution();
            evolutions = new ArrayList<Evolution>();
        }

    }

    public void getHistoryRecords() {
        recordses = recordsService.getHistoryRecords(customer);
        if(!recordses.isEmpty()){
            lastRecords = recordses.get(0);
        }
        
        
    }

    public int getAge() {
        int idade = 0;
        if (customer.getDateBirth() != null) {
            Calendar c = new GregorianCalendar();
            Calendar dateBirth = new GregorianCalendar();
            dateBirth.setTime(customer.getDateBirth());

            idade = c.get(Calendar.YEAR) - dateBirth.get(Calendar.YEAR);

            dateBirth.add(Calendar.YEAR, idade);

            if (c.before(dateBirth)) {
                idade--;
            }
        }
        return idade;

    }

    public Diagnostic getDiagnostic() {
        return diagnostic;
    }

    public void setDiagnostic(Diagnostic diagnostic) {
        this.diagnostic = diagnostic;
    }

    public PhysicalExamination getPhysicalExamination() {
        return physicalExamination;
    }

    public void setPhysicalExamination(PhysicalExamination physicalExamination) {
        this.physicalExamination = physicalExamination;
    }

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public Anamnese getAnamnese() {
        return anamnese;
    }

    public void setAnamnese(Anamnese anamnese) {
        this.anamnese = anamnese;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Records getRecords() {
        return records;
    }

    public void setRecords(Records records) {
        this.records = records;
    }

    public void handle(FileUploadEvent event) throws IOException {
        handleAux(event.getFile().getContents(), event.getFile().getFileName(),
                event.getFile().getContentType(), event.getFile().getInputstream());
    }

    private void handleAux(byte[] imageBytes, String nome, String tipo, InputStream is) throws IOException {

        byte[] bytes = null;

        Annex annex = new Annex();
        bytes = imageBytes;
        annex.setType(tipo);
        annex.setDescription(nome);
        if (bytes != null) {
            annex.setFileByte(bytes);
            annexService.save(annex);
            System.err.println("Anexo" + annex);
            annexs.add(annex);
            annex = new Annex();
        }
    }

    public List<Annex> getAnnexs() {
        return annexs;
    }

    public void setAnnexs(List<Annex> annexs) {
        this.annexs = annexs;
    }

    public List<Anamnese> getAnamneses() {
        return anamneses;
    }

    public void setAnamneses(List<Anamnese> anamneses) {
        this.anamneses = anamneses;
    }

    public Evolution getEvolution() {
        return evolution;
    }

    public void setEvolution(Evolution evolution) {
        this.evolution = evolution;
    }

    public List<Evolution> getEvolutions() {
        return evolutions;
    }

    public void setEvolutions(List<Evolution> evolutions) {
        this.evolutions = evolutions;
    }

    public List<Records> getRecordses() {
        return recordses;
    }

    public void setRecordses(List<Records> recordses) {
        this.recordses = recordses;
    }

    public TabView getTabView() {
        return tabView;
    }

    public void setTabView(TabView tabView) {
        this.tabView = tabView;
    }

    public void lastPrescriptionRecords() {
        records.setPrescription(recordsService.getLastPrescriptionRecords(customer));
    }

    public void reportPreescription() {
        if (doctor != null) {
            records.setDoctor(doctor);
        }
        records.setCustomer(customer);
        if (records.getId() == null) {
            records.setCreateAt(new Date());
            recordsService.save(records);
        } else {
            recordsService.update(records);
        }

        recordsService.reportsPrescription(records);
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
    
    public void searchCustomer(){
        customer.setCpf(Caracter.removecaracter(customer.getCpf()));
        customers = customerService.findCustomer(customer);
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public Diary getLastDiary() {
        return lastDiary;
    }

    public void setLastDiary(Diary lastDiary) {
        this.lastDiary = lastDiary;
    }

    public Procedure getProcedure() {
        return procedure;
    }

    public void setProcedure(Procedure procedure) {
        this.procedure = procedure;
    }

    public Records getLastRecords() {
        return lastRecords;
    }

    public void setLastRecords(Records lastRecords) {
        this.lastRecords = lastRecords;
    }
    


    public void reportRecords() {
        recordsService.reportsRecords(customer);
    }

    public Procedure getProcedureNewRecords() {
        return procedureNewRecords;
    }

    public void setProcedureNewRecords(Procedure procedureNewRecords) {
        this.procedureNewRecords = procedureNewRecords;
    }
    
    
}
